package xyz.zhzby.cyzx.acl.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.zhzby.cyzx.acl.entity.Permission;
import xyz.zhzby.cyzx.acl.service.IPermissionService;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

import java.util.List;

/**
 * @author zby
 * @date 2021/12/07
 */
@RestController
@RequestMapping("/acl/permission")
//@CrossOrigin
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @ApiOperation("添加菜单")
    @PostMapping("")
    public RestResponse<String> saveOne(@RequestBody Permission permission) {
        return permissionService.saveOne(permission);
    }

    @ApiOperation("修改菜单")
    @PutMapping("")
    public RestResponse<String> updateOne(@RequestBody Permission permission) {
        return permissionService.updateOne(permission);
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("")
    public RestResponse<List<Permission>> indexAllPermission() {
        return permissionService.queryAllMenu();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/assign")
    public RestResponse<String> doAssign(Long roleId, Long[] permissionId) {
        permissionService.saveRolePermissionRealtionShip(roleId, permissionId);
        return RestResponse.success("成功");
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/assign/{roleId}")
    public RestResponse<List<Permission>> toAssign(@PathVariable("roleId") Long roleId) {
        return permissionService.selectAllMenu(roleId);
    }

    @ApiOperation("根据ID删除菜单")
    @DeleteMapping("/{permissionId}")
    public RestResponse<String> removeById(@PathVariable("permissionId") Long permissionId) {
        return permissionService.removePermissionById(permissionId);
    }

}

